from models import Photo, PhotoNotification
from datetime import datetime,timedelta


class AddPhotoRequest:
    def __init__(self,community_id,source,title,thumb_url,medium_url,original_url,meta):
        self.community_id = community_id.lower()
        self.source = source
        self.title = title
        self.thumb_url = thumb_url
        self.medium_url = medium_url
        self.original_url = original_url
        self.meta = meta

class GetPhotosRequest:
    def __init__(self,community_id, count = None):
        self.community_id = community_id.lower()
        self.count = count

class GetNotificationsRequest:
    def __init__(self,communityId):
        self.communityId = communityId.lower()

class NotificationGroup:
    def __init__(self,type,date):
        self.type = type
        self.date = date
        self.photos = []
    
    def getGroupSize(self):
        return len(self.photos)
    groupSize = property(getGroupSize)
        
    def belongsToGroup(self, notification):
        return (abs((self.date - notification.added).days) < 1) and (notification.notification == self.type) 
    
    def addNotification(self,notification):
        self.photos.append(notification.photo)
        
    @staticmethod
    def buildGroups(notifications):
        """
            Takes a list of notifications and outputs a list of notification groups
        """
        
        #identify distinct dates (same day == same date)
        dates = []
        for n in notifications:
            d = n.added
            isDistinct = True
            for dd in dates:
                if abs((dd - d).days) < 1:
                    isDistinct = False
                    break  
            if isDistinct: dates.append(d) 
        
        #build groups corresponding to dates and actions 
        groups = []
        for d in dates: groups.append(NotificationGroup("added", d)) 
        
        for n in notifications:
            for g in groups:
                if g.belongsToGroup(n): g.addNotification(n)
                
        return groups
        

class PhotoService:
    def __init__(self):
        pass
    
    def getPhotoById(self,id):
        return Photo.objects.get(id=id)
    
    def getPhotos(self,request):
        #TODO:adapt for more queries, currently only works for community id
        if request.count is not None:
            return Photo.objects.filter(community_id=request.community_id)[:request.count]
        else:
            return Photo.objects.filter(community_id=request.community_id) 
    
    def getNotifications(self,request):
        # OUTPUT : list of NotificationGroup elements
        # notifications are grouped by type and date (one group per day)
        notifications = PhotoNotification.objects.filter(community_id=request.communityId).order_by("-added")
        return NotificationGroup.buildGroups(notifications)
    
    def addPhoto(self,request):
        photo = Photo()
        # TODO: adapt this to multiple sources
        photo.community_id = request.community_id
        photo.source = request.source 
        photo.title = request.title
        photo.thumb_url = request.thumb_url
        photo.medium_url = request.medium_url
        photo.original_url = request.original_url
        photo.meta = request.meta 
        photo.save()
        
        #add notification
        notification = PhotoNotification()
        notification.community_id = request.community_id
        notification.photo = photo
        notification.notification = PhotoNotification.NOTIFICATION_ADDED
        notification.save()
        
        return photo